package cac.fenjian.resource.partSalesReturn.rest;


import cac.fenjian.resource.WmsDeliverPlan.dto.WmsDeliverPlanDTO;
import cac.fenjian.resource.WmsReturnApply.dto.WmsReturnApplyDTO;
import cac.fenjian.resource.partSalesReturn.service.PartSalesReturnService;
import com.alibaba.fastjson.JSON;
import com.cac.dme.adapter.rest.msg.PageParameter;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.adapter.utils.ContextCommonHolder;
import com.cac.dme.common.utils.JsonHelper;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.cac.dme.adapter.utils.BusinessUtil.getSortExpColumnName;

@RestController
@RequestMapping("/partSalesReturn/")
public class PartSalesReturnRest {
    @Autowired
    private PartSalesReturnService partSalesReturnService;

    /**
     * 创建退货单
     * @param map
     * @return
     */
    @PostMapping("/creatReturnSales")
    public ResponseMsg<Integer> creatReturnSales(@RequestBody Map map){
        ResponseMsg<Integer> responseMsg =  new ResponseMsg<>();
        int msg = partSalesReturnService.insertWmsReturnApply(map);
        if(msg == 1){
            responseMsg.setMessage("成功创建退货单！").setRetCode("200");
        }else if (msg == -2){
            responseMsg.setMessage("创建退货单失败！退库数量大于现有库存数量").setRetCode("201");
        }else if (msg == -3){
            responseMsg.setMessage("创建退货单失败！退库数量不能包含已被非离库占用的数量！").setRetCode("201");
        }else{
            responseMsg.setMessage("创建退货单失败！").setRetCode("201");
        }
        return responseMsg;
    }
    //零件退货页面条件查询
    @PostMapping("/searchParamsByReturn")
    public ResponseMsg<QueryRespBean<WmsReturnApplyDTO>> searchParamsBy(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<WmsReturnApplyDTO>> responseMsg = new ResponseMsg<>();
        //时间范围
        String startTimeParams = JSON.toJSONString(map.get("startTime"));
        String endTimeParams = JSON.toJSONString(map.get("endTime"));
        Date startTime = JSON.parseObject(startTimeParams, Date.class);
        Date endTime = JSON.parseObject(endTimeParams, Date.class);
        //分页
        String param = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(param, PageParameter.class);
        //查询条件
        String searchParamsParams = JSON.toJSONString(map.get("searchParams"));
        WmsReturnApplyDTO searchParams = JSON.parseObject(searchParamsParams, WmsReturnApplyDTO.class);

        QueryReqBean<WmsReturnApplyDTO> queryReqBean = new QueryReqBean<>();
        queryReqBean.setSearchParams(searchParams);
        queryReqBean.setPageParameter(pageParameter);

        if (StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())) {
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(), queryReqBean.getSord(), WmsReturnApplyDTO.class);
            if (StringUtils.hasText(sordExp)) {
                queryReqBean.setSortExp(sordExp);
            }
        }
        if (StringUtils.hasText(queryReqBean.getKeyWord())) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            WmsReturnApplyDTO searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<WmsReturnApplyDTO>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }

        QueryRespBean<WmsReturnApplyDTO> result = partSalesReturnService.selectReturnApply(queryReqBean, ContextCommonHolder.getOrgId(), queryReqBean.getSortExp(), startTime, endTime);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }
    //成品退货页面条件查询
    @PostMapping("/searchParamsByProductReturn")
    public ResponseMsg<QueryRespBean<WmsReturnApplyDTO>> searchParamsByProduct(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<WmsReturnApplyDTO>> responseMsg = new ResponseMsg<>();
        //时间范围
        String startTimeParams = JSON.toJSONString(map.get("startTime"));
        String endTimeParams = JSON.toJSONString(map.get("endTime"));
        Date startTime = JSON.parseObject(startTimeParams, Date.class);
        Date endTime = JSON.parseObject(endTimeParams, Date.class);
        //分页
        String param = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(param, PageParameter.class);
        //查询条件
        String searchParamsParams = JSON.toJSONString(map.get("searchParams"));
        WmsReturnApplyDTO searchParams = JSON.parseObject(searchParamsParams, WmsReturnApplyDTO.class);

        QueryReqBean<WmsReturnApplyDTO> queryReqBean = new QueryReqBean<>();
        queryReqBean.setSearchParams(searchParams);
        queryReqBean.setPageParameter(pageParameter);

        if (StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())) {
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(), queryReqBean.getSord(), WmsReturnApplyDTO.class);
            if (StringUtils.hasText(sordExp)) {
                queryReqBean.setSortExp(sordExp);
            }
        }
        if (StringUtils.hasText(queryReqBean.getKeyWord())) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            WmsReturnApplyDTO searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<WmsReturnApplyDTO>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }

        QueryRespBean<WmsReturnApplyDTO> result = partSalesReturnService.selectReturnApplyProduct(queryReqBean, ContextCommonHolder.getOrgId(), queryReqBean.getSortExp(), startTime, endTime);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }
    //零件配送页面条件查询
    @PostMapping("/searchParamsByDelivery")
    public ResponseMsg<QueryRespBean<WmsDeliverPlanDTO>> searchParamsByDelivery(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<WmsDeliverPlanDTO>> responseMsg = new ResponseMsg<>();
        //分页
        String param = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(param, PageParameter.class);
        //查询条件
        String searchParamsParams = JSON.toJSONString(map.get("searchParams"));
        WmsDeliverPlanDTO searchParams = JSON.parseObject(searchParamsParams, WmsDeliverPlanDTO.class);

        QueryReqBean<WmsDeliverPlanDTO> queryReqBean = new QueryReqBean<>();
        queryReqBean.setSearchParams(searchParams);
        queryReqBean.setPageParameter(pageParameter);

        if (StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())) {
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(), queryReqBean.getSord(), WmsDeliverPlanDTO.class);
            if (StringUtils.hasText(sordExp)) {
                queryReqBean.setSortExp(sordExp);
            }
        }
        if (StringUtils.hasText(queryReqBean.getKeyWord())) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            WmsDeliverPlanDTO searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<WmsDeliverPlanDTO>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }

        QueryRespBean<WmsDeliverPlanDTO> result = partSalesReturnService.selectDeliverPlan(queryReqBean, ContextCommonHolder.getOrgId(), queryReqBean.getSortExp());
        responseMsg.setResponseBody(result);
        return responseMsg;
    }
    //成品配送页面条件查询
    @PostMapping("/searchParamsByDeliveryProduct")
    public ResponseMsg<QueryRespBean<WmsDeliverPlanDTO>> searchParamsByDeliveryProduct(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<WmsDeliverPlanDTO>> responseMsg = new ResponseMsg<>();
        //分页
        String param = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(param, PageParameter.class);
        //查询条件
        String searchParamsParams = JSON.toJSONString(map.get("searchParams"));
        WmsDeliverPlanDTO searchParams = JSON.parseObject(searchParamsParams, WmsDeliverPlanDTO.class);

        QueryReqBean<WmsDeliverPlanDTO> queryReqBean = new QueryReqBean<>();
        queryReqBean.setSearchParams(searchParams);
        queryReqBean.setPageParameter(pageParameter);

        if (StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())) {
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(), queryReqBean.getSord(), WmsDeliverPlanDTO.class);
            if (StringUtils.hasText(sordExp)) {
                queryReqBean.setSortExp(sordExp);
            }
        }
        if (StringUtils.hasText(queryReqBean.getKeyWord())) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            WmsDeliverPlanDTO searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<WmsDeliverPlanDTO>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }

        QueryRespBean<WmsDeliverPlanDTO> result = partSalesReturnService.selectDeliverPlanProduct(queryReqBean, ContextCommonHolder.getOrgId(), queryReqBean.getSortExp());
        responseMsg.setResponseBody(result);
        return responseMsg;
    }
    //修改退货单数据
    @PostMapping("/updateReturnApply")
    public ResponseMsg<Integer> update(@RequestBody WmsReturnApplyDTO wmsReturnApplyDTO){
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(partSalesReturnService.updateWmsReturnApply(wmsReturnApplyDTO));
        return responseMsg;
    }

    //删除退货单数据
    @PostMapping("/deleteReturnApplyByIds")
    public ResponseMsg<Map> deleteByIds(@RequestBody HashMap map) {
        ResponseMsg<Map> responseMsg = new ResponseMsg<>();
        Map resMap = new HashMap();
        int  s=0,f=0;
        List<HashMap> listmap = (List<HashMap>) map.get("data");
        if(listmap.size()==0){
            return responseMsg.setRetCode("201").setMessage("请选中需要删除的数据");
        }
        for( int i=0;i<listmap.size();i++)
        {
            HashMap<String,Object> map1;
            map1=listmap.get(i);
            if("编辑中".equals(map1.get("returnStatus")))
            {
                partSalesReturnService.deleteWmsReturnApplyByIds(map1);
                s++;

            }
            else
            {
                f++;
            }
        }
        if(s==0&&f!=0)
        {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("删除失败！");
        }
        if(f==0&&s!=0)
        {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("删除成功！");
        }
        else
        {
            resMap.put("成功数",s);
            resMap.put("失败数",f);
            responseMsg.setResponseBody(resMap);
            responseMsg.setRetCode("202");
        }


        //        java.util.List<String> wmsReturnApplyIds  = (java.util.List<String>) map.get("wmsReturnApplyId");
//        java.util.List<String> returnStatus  = (java.util.List<String>) map.get("returnStatus");
//
//
        return responseMsg;
    }
    //提交退货单信息
    @PostMapping("/submitReturnSales")
    public  ResponseMsg<Map> submitReturn(@RequestBody HashMap<String,Object> maps) {
        ResponseMsg<Map> responseMsg = new ResponseMsg<>();
        Map reMsg = new HashMap();
        int s = 0;
        int f = 0;
        List<HashMap<String, Object>> listMap = (List<HashMap<String, Object>>) maps.get("data");
        if(listMap.size()==0){
            return responseMsg.setRetCode("201").setMessage("请选择退货单");
        }
        for (int i = 0; i < listMap.size(); i++) {
            HashMap<String, Object> map = listMap.get(i);
            if ("编辑中".equals(map.get("returnStatus"))) {
                partSalesReturnService.changeReturnType((String) map.get("wmsReturnApplyId"));
                s++;
            } else f++;
        }
        reMsg.put("成功：", s);
        reMsg.put("失败：", f);
        if (s == 0 && f != 0) {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("提交失败！");

        }
        if (s != 0 && f == 0) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("提交成功！");
        }
        if (s != 0 && f != 0) {
            responseMsg.setRetCode("202");
            responseMsg.setMessage("提交有失败项！");
            responseMsg.setResponseBody(reMsg);
        }
        return responseMsg;
    }

    @PostMapping("/searchWorkbinMaterial")
    public List<HashMap> searchWorkbinMaterial(@RequestBody HashMap map){
        return partSalesReturnService.searchWorkbinMaterial(map);
    }
}
